"""
@author: liujun
@file: sanle_shopping.py.py
@time: 2022/11/18 11:23
"""

#1、登录
#2、注册
# user = [["sc","123456",100],["sc1","sc123",200]]
#用户信息： 用户名 密码  余额
#user = {"sc":{"passwd":"123456", "money":100}}
#商品信息： 价格 名字  编号
products = [["苹果", 5],["香蕉", 3], ['葡萄', 8], ["榴莲", 40]]
#购物车： 用户名 商品编号 数量
#car={“sc”:[[0, 2], [1, 3]]}
car={}
user = {}
def login(name, passwd):
    import json
    fp = open("usermessage.txt")
    user_str = fp.read()
    user = json.loads(user_str)
    if name in user and passwd == user[name]["passwd"]:
        return True
def regist(name, passwd1,passwd2):
    if name in user:
        print("用户名已存在")
        return False
    if passwd1 != passwd2:
        print("两次密码不一致")
        return False
    if name not in user and passwd1==passwd2:
        print("用户创建成功")
        user[name]={"passwd":passwd1,"money":100}
        import json
        user_str = json.dumps(user)
        fp = open("usermessage.txt", "w")
        fp.write(user_str)
        fp.close()
        return True
    print(user)

def show_goods():
    print("编号".ljust(10, " "), "名称".ljust(10, " "), "价格".ljust(10, " "))
    for index, pro in enumerate(products):
        print(str(index).ljust(11, " "),pro[0].ljust(10," "),str(pro[1]).ljust(10, " "))

def add_car(name, product_id, nums):
    # car={“sc”:[[0, 2], [1, 3]]}
    if name not in car:
        car[name] = []
    car[name].append([product_id, nums])
    print("你的用户信息：",car[name])

def checkout(name):
    #user = {"sc": {"passwd": "123456", "money": 0}}
    m1 = user[name]["money"]  #用户的钱
    car_list = car[name]  #[[0, 2], [1, 3]]
    # car={“sc”:[[0, 2], [1, 3]]}
    #products = [["苹果", 5], ["香蕉", 3], ['葡萄', 8], ["榴莲", 40]]
    total = 0
    print("编号  ", "名称  ", "单价", "数量", "总价")
    for pro in car_list:
        #pro -->[1, 3] #[商品编号， 商品数量]
        pro_name = products[pro[0]][0]
        price = products[pro[0]][1]
        pro_total = price * pro[1]
        total += pro_total
        print(pro[0],pro_name,price, pro[1], pro_total)
    print("所有商品总价为：", total)
    if m1 >= total:
        m1 -= total
    else:
        print("余额不足！")
    #修改用户金额
    user[name]["money"] = m1
    print(user)

def menu():
    print("*"*20)
    print("1、查看商品列表")
    print("2、添加到购物车")
    print("3、结算")
    print("4、退出")


print("欢迎来到三乐购物系统".center(50,"*"))
# print("1.登录")
# print("2.注册")
# options = input("请输入你的选择：")
while 1:
    print("1.登录")
    print("2.注册")
    options = input("请输入你的选择(按q退出)：")
    if options == "1":
        name = input("请输入用户名:")
        passwd = input("请输入密码:")
        if login(name, passwd):
            while 1:
                menu()
                op = input("请输入你的操作:")
                if op == "1":
                    show_goods()
                elif op == "2":
                    product_id = input("请输入添加到购物车的商品编号：")
                    if product_id.isdigit() and 0<=int(product_id)<len(products):
                        nums = int(input("请输入需要购买的数量:"))
                        add_car(name, int(product_id), nums)
                elif op == "3":
                    checkout(name)
                elif op == "4":
                    break
        else:
            print("登录失败！ 用户名或密码错误！")
    elif options == "2":
        while 1:
            name=input("请输入用户名：")
            passwd1=input("请输入密码：")
            passwd2=input("请再次输入密码：")
            regist = regist(name,passwd1,passwd2)
            if regist:
                break
    elif options == "q":
        break
    else:
        print("输入不合法！")
import requests

picture = {"https://www.sanchuangedu.cn/wgyc.png","https://www.sanchuangedu.cn/luogan.png","https://www.sanchuangedu.cn/xiayuzhen.jpg"}

result = requests.get("https://www.sanchuangedu.cn/wgyc.png")
with open("../test/teacher-shushan/2022-11-18-文件操作/liujun'/1.jpg", "wb") as fp:
    fp.write(result.content)

result = requests.get("https://www.sanchuangedu.cn/luogan.png")
with open("../test/teacher-shushan/2022-11-18-文件操作/liujun'/2.jpg", "wb") as fp:
    fp.write(result.content)

result = requests.get("https://www.sanchuangedu.cn/xiayuzhen.jpg")
with open("../test/teacher-shushan/2022-11-18-文件操作/liujun'/3.jpg", "wb") as fp:
    fp.write(result.content)